/*
 * @Author: zhengzhuang
 * @since: 2020-02-29 17:39:19
 * @lastTime: 2020-03-01 14:24:27
 * @LastAuthor: zhengzhuang
 * @FilePath: /C20Driver/lib/pages/carpool_page/take_passengers.dart
 * @message: 捎上乘客   TakePassengers
 */
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/pages/carpool_page/carpool_trip.dart';
import 'package:C20Driver/provider/position_information.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/permission_handler.dart';
import 'package:C20Driver/utils/utils.dart';
import 'package:C20Driver/widget/spinkit.dart';
import 'package:provider/provider.dart';

class TakePassengers extends StatefulWidget {
  final String id;
  final String orderId;
  TakePassengers({this.id, this.orderId});
  @override
  _TakePassengersState createState() => _TakePassengersState();
}

class _TakePassengersState extends State<TakePassengers> {
  AmapController _amapController;
  List<Marker> _markers = [];

  var _getData;

  @override
  void initState() {
    super.initState();
    _getData = _getUserRideOrderInfo();
  }

  Future _getUserRideOrderInfo() async {
    return await Request.getUserRideOrderInfoFun(widget.id);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('捎上乘客'),
      ),
      body: FutureBuilder(
        future: _getData,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            var snapData = snapshot.data;
            return Container(
                child: Stack(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(bottom: ScreenUtil().setWidth(400.0)),
                  child: AmapView(
                    showZoomControl: false,
                    showCompass: true,
                    // 中心点坐标
                    centerCoordinate: LatLng(
                        num.parse(snapData["dep_latitude"]),
                        num.parse(snapData["dep_longitude"])),
                    // 缩放级别
                    onMapCreated: (controller) async {
                      _amapController = controller;
                      if (await PermissionHandlerX.position(context)) {
                        /// 两点直线距离
                        await _amapController?.addPolyline(PolylineOption(
                          latLngList: [
                            LatLng(
                                Provider.of<PositionInformation>(context,
                                        listen: false)
                                    .driverLocation
                                    .latitude,
                                Provider.of<PositionInformation>(context,
                                        listen: false)
                                    .driverLocation
                                    .longitude),
                            LatLng(num.parse(snapData["dep_latitude"]),
                                num.parse(snapData["dep_longitude"])),
                          ],
                          width: 10,
                          strokeColor: Colors.green,
                        ));

                        markerList = [
                          {
                            'iconImage': 'assets/icon/startingPointIcon.png',
                            'laction': LatLng(
                                Provider.of<PositionInformation>(context,
                                        listen: false)
                                    .driverLocation
                                    .latitude,
                                Provider.of<PositionInformation>(context,
                                        listen: false)
                                    .driverLocation
                                    .longitude),
                          },
                          {
                            'iconImage': 'assets/icon/endIcon.png',
                            'laction': LatLng(
                                num.parse(snapData["dep_latitude"]),
                                num.parse(snapData["dep_longitude"])),
                          },
                        ];

                        /// 设置两点图片
                        final markers = await _amapController?.addMarkers(
                          [
                            for (int i = 0; i < 2; i++)
                              MarkerOption(
                                latLng: markerList[i]['laction'],
                                widget: Container(
                                  width: ScreenUtil().setWidth(100.0),
                                  height: ScreenUtil().setWidth(100.0),
                                  alignment: Alignment.topCenter,
                                  child: Image.asset(
                                    markerList[i]['iconImage'],
                                    width: ScreenUtil().setWidth(50.0),
                                    height: ScreenUtil().setWidth(50.0),
                                    fit: BoxFit.contain,
                                  ),
                                ),
                                anchorV: 0.5,
                                anchorU: 0.5,
                                draggable: true,
                              ),
                          ],
                        );
                        _markers.addAll(markers);

                        /// 将指定的经纬度列表(包括但不限于marker, polyline, polygon等)调整至同一屏幕中显示
                        List<LatLng> arr = [
                          LatLng(
                              Provider.of<PositionInformation>(context,
                                      listen: false)
                                  .driverLocation
                                  .latitude,
                              Provider.of<PositionInformation>(context,
                                      listen: false)
                                  .driverLocation
                                  .longitude),
                          LatLng(num.parse(snapData["dep_latitude"]),
                              num.parse(snapData["dep_longitude"])),
                        ];
                        _amapController?.zoomToSpan(arr,
                            padding:
                                EdgeInsets.all(ScreenUtil().setWidth(200.0)));
                      }
                    },
                    onMapClicked: (latLng) {
                      return;
                    },
                  ),
                ),
                Positioned(
                  bottom: 0,
                  left: ScreenUtil().setWidth(30.0),
                  child: _pickUpOperation(snapData),
                ),
                // NoPassengersReceived(),
                // SafeArea(
                //   child: Container(
                //       height: ScreenUtil().setWidth(80.0),
                //       padding: EdgeInsets.only(left: ScreenUtil().setWidth(10.0)),
                //       child: InkWell(
                //         child: Icon(Icons.navigate_before,
                //             size: ScreenUtil().setSp(60.0)),
                //         onTap: () =>  Navigator.of(context).popUntil((route) => route.isFirst),
                //       )),
                // ),
              ],
            ));
          } else {
            return Spinkit();
          }
        },
      ),
    );
  }

  /// 操作
  Widget _pickUpOperation(dataSnap) {
    return Container(
      width: ScreenUtil().setWidth(690.0),
      height: ScreenUtil().setWidth(499.0),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(ScreenUtil().setWidth(20.0)),
          boxShadow: <BoxShadow>[
            BoxShadow(
              color: Colors.black12,
              blurRadius: ScreenUtil().setWidth(10.0),
            ),
          ]),
      child: Column(
        children: <Widget>[
          SizedBox(height: ScreenUtil().setWidth(20.0)),
          Row(
            children: <Widget>[
              SizedBox(width: ScreenUtil().setWidth(20.0)),
              ClipOval(
                child: Image.network(
                  // '${providerOrder.passengerHeadimgurl}',
                  '${dataSnap["headimgurl"]}',
                  width: ScreenUtil().setWidth(100.0),
                  height: ScreenUtil().setWidth(100.0),
                ),
              ),
              SizedBox(width: ScreenUtil().setWidth(20.0)),
              Container(
                width: ScreenUtil().setWidth(500.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('${dataSnap["passenger_name"]}',
                        style: TextStyle(
                            fontSize: ScreenUtil().setSp(36.0),
                            color: Colors.black,
                            fontWeight: FontWeight.w700)),
                    Text(
                        '${dataSnap["departure_date"]}  ${dataSnap["people_number"]}人',
                        style: TextStyle(
                            fontSize: ScreenUtil().setSp(36.0),
                            color: Color.fromARGB(255, 248, 169, 5),
                            fontWeight: FontWeight.w700)),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: ScreenUtil().setWidth(20.0)),
          Divider(),
          SizedBox(height: ScreenUtil().setWidth(20.0)),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              // Column(
              //   children: <Widget>[
              //     Icon(Icons.ac_unit, size: ScreenUtil().setSp(60.0)),
              //     Text('发消息',
              //         style: TextStyle(
              //             fontSize: ScreenUtil().setSp(24.0),
              //             color: Colors.black)),
              //   ],
              // ),
              InkWell(
                  child: Column(
                    children: <Widget>[
                      Icon(Icons.near_me, size: ScreenUtil().setSp(60.0)),
                      Text('导航',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(24.0),
                              color: Colors.black)),
                    ],
                  ),
                  onTap: () => Utils.openGaode(
                      lat: dataSnap["dep_latitude"],
                      lng: dataSnap["dep_longitude"],
                      name: dataSnap["dep_area"],
                      context: context)),
              InkWell(
                child: Column(
                  children: <Widget>[
                    Icon(Icons.call, size: ScreenUtil().setSp(60.0)),
                    Text('打电话',
                        style: TextStyle(
                            fontSize: ScreenUtil().setSp(24.0),
                            color: Colors.black)),
                  ],
                ),
                onTap: () => Utils.callPhone(dataSnap["mobile"].toString()),
              ),
            ],
          ),
          SizedBox(height: ScreenUtil().setWidth(50.0)),
          MaterialButton(
            child: Text('捎上乘客',
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(36.0), color: Colors.white)),
            color: Color.fromARGB(255, 255, 103, 27),
            minWidth: ScreenUtil().setWidth(640.0),
            height: ScreenUtil().setWidth(100.0),
            // onPressed: () => dataSnap.driverArriveBoardingPoint(context),
            onPressed: () => Request.driverArriveBoardingPoint(
                cId: widget.id, sjId: widget.orderId, context: context),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _amapController.dispose();
    // _amapController = null;
    super.dispose();
  }
}
